from tool.RestTemplate import RestTemplate

geo_url = "https://restapi.amap.com/v3/geocode/geo"
geo_params = {'key': '58b8527d0f9ed3264778f088c348ccb6', 'address': '上海', 'output': 'JSON'}

# ad_code = requests.get(url=geo_url, params=geo_params).json().get("geocodes")[0].get("adcode")

restTemplate = RestTemplate()
response = restTemplate.get(url=geo_url, params=geo_params)
print(type(response))
print(response.content)
print(response.text)
print(response.headers)
# 内部使用 json.loads();
data = response.json()
# <class 'dict'>
print(type(data))
# {'status': '1', 'info': 'OK', 'infocode': '10000', 'count': '1',
# 'geocodes': [{'formatted_address': '上海市', 'country': '中国', 'province': '上海市', 'citycode': '021', 'city': '上海市', 'district': [], 'township': [],
# 'neighborhood': {'name': [], 'type': []}, 'building': {'name': [], 'type': []}, 'adcode': '310000', 'street': [], 'number': [], 'location': '121.473667,31.230525', 'level': '省'}]}

ad_code = data.get("geocodes")[0].get("adcode")
print(ad_code)

weather_url = "https://restapi.amap.com/v3/weather/weatherInfo"
weather_params = {'key': '58b8527d0f9ed3264778f088c348ccb6', 'city': ad_code, 'extensions': 'base', 'output': 'JSON'}

lives_ = restTemplate.get(weather_url, params=weather_params).json().get("lives")[0]

weather = lives_.get("weather")
temperature = lives_.get("temperature")
print(weather)
print(temperature)
